---
title: Register MonoBehaviour
---

## Register from LifetimeScope's `[SerializeField]`

```csharp
[SerializeField]
YourBehaviour yourBehaviour;

// ...

builder.RegisterComponent(yourBehaviour);
```

:::note
RegisterComponent are similar to RegisterInstance. The only difference is that `MonoBehaviour`s registered with RegisterComponent will be injected even if not Resolved.
:::

## Register from scene with `LifetimeScope`

```csharp
builder.RegisterComponentInHierarchy<YourBehaviour>();
```

:::note
RegisterComponentInHierarchy always have a `Scoped` lifetime because the lifetime is equal to the scene.
:::

## Register component that Instantiate from prefab when resolving

```csharp
[SerializeField]
YourBehaviour prefab;

// ...

builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped);
```

## Register component that with new GameObject when resolving

```csharp
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "NewGameObjectName");
```

## Register component as interface

```csharp
builder.RegisterComponentInHierarchy<YourBehaviour>()
    .AsImplementedInterfaces();
```

## Register component to specific parent Transform

```csharp
// Instantiate new GameObject under the specified transform
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped)
    .UnderTransform(parent);

// Instantiate new prefab under the specified transform
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped)
    .UnderTransform(parent);

// Find the component under the specified transform.
builder.RegisterComponentInHierarchy<YourBehaviour>()
    .UnderTransform(parent);
```

Or find at runtime.

```csharp
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped)
    .UnderTransform(() => {
        // ...
        return parent;
    });

```


## Register as `DontDestroyOnLoad`

RegsiterComponent* method chain can be DontDestroyOnLoad.

```csharp
// Instantiate new GameObject as DontDestroyOnLoad
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped)
    .DontDestroyOnLoad();

// Instantiate new prefab as DontDestroyOnLoad
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped)
    .DontDestroyOnLoad();
```

## Grouping MonoBehaviour's Registration

```csharp
builder.UseComponents(components =>
{
    components.AddInstance(yourBehaviour);
    components.AddInHierarchy<YourBehaviour>();
    components.AddFromNewPrefab(prefab, Lifetime.Scoped);
    components.AddOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");
});
```

This is the same as:

```csharp
builder.RegisterComponent(yourBehaviour);
builder.RegisterComponentInHierarchy<YourBehaviour>();
builder.RegisterComponentFromNewPrefab(prefab, Lifetime.Scoped);
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");
```

You can create a group with a specified parent.

```csharp
builder.UseComponents(parentTransform, components =>
{
    // GetComponentInChildren under `parentTransform`
    components.AddInHierarchy<YourBehaviour>();

    // Instantiate under `parentTransform`
    components.AddFromNewPrefab(prefab, Lifetime.Scoped);
    components.AddOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");
})
```

This is the same as:

```csharp
builder.RegisterComponentInHierarchy<YourBehaviour>()
    .UnderTransform(parentTransform);

builder.RegisterComponentFromNewPrefab(prefab, Lifetime.Scoped)
    .UnderTransform(parentTransform);
builder.RegisterComponentOnNewGameObject<YourBehaviour>(Lifetime.Scoped, "name");
    .UnderTransform(parentTransform);
```
